import { DataSource, ConnectionOptions, DataSourceOptions } from "typeorm";
import path from 'node:path';
import { getEnvBoolean, getEnvNumber, getEnvString } from "../utils/envUtil";
import { BooleanEnvEnum, NumberEnvEnum, StringEnvEnum } from "../constants/env";

export const databaseConfig: DataSourceOptions = {
  type: getEnvString(StringEnvEnum.DB_TYPE) as any,
  host: getEnvString(StringEnvEnum.DB_HOST),
  port: getEnvNumber(NumberEnvEnum.DB_PORT),
  username: getEnvString(StringEnvEnum.DB_USERNAME),
  password: getEnvString(StringEnvEnum.DB_PASSWORD),
  database: getEnvString(StringEnvEnum.DB_DATABASE),
  synchronize: getEnvBoolean(BooleanEnvEnum.DB_SYNCHRONIZE),
  logging: getEnvBoolean(BooleanEnvEnum.DB_LOGGING),
  entities: [path.resolve(__dirname, '../entity/*.ts')],
  migrations: [],
  subscribers: [],
}

export const AppDataSource = new DataSource(databaseConfig);
